﻿using System.Linq;
using System.Data.Linq;
using UniMana.Data;
using System;
namespace UniMana.Data
{
    public static class Training_Subject_Class_GroupData
    {
        /// <summary>
        /// lấy danh sách lớp ghép theo tình trạng
        /// </summary>
        /// <param name="subject_Class_Group"></param>
        /// <param name="Opened"></param>
        /// <returns></returns>
        public static IQueryable 
            Get_Opened(this Table<Training_Subject_Class_Group> subject_Class_Group,
                        int Opened = 1)
        {
            switch (Opened)
            {
                case 1:
                    {
                        var Subject_Class_Group = from sjcg in subject_Class_Group
                                                  where sjcg.Opened == true
                                                  select sjcg;
                        return Subject_Class_Group.Distinct();
                    };
                case 0:
                    {
                        var Subject_Class_Group = from sjcg in subject_Class_Group
                                                  where sjcg.Opened == false || sjcg.Opened == null
                                                  select sjcg;
                        return Subject_Class_Group.Distinct();
                    };
                default:
                    {
                        var Subject_Class_Group = from sjcg in subject_Class_Group
                                                  select sjcg;
                        return Subject_Class_Group.Distinct();
                    }
            }
        }
        /// <summary>
        /// cập nhật tình trạng lớp ghép
        /// </summary>
        /// <param name="subject_Class_Group"></param>
        /// <param name="Subject_Class_GroupKey"></param>
        /// <param name="Opened"></param>
        public static void Update_Subject_Class_Group_Opened(
            this Table<Training_Subject_Class_Group> subject_Class_Group,
            int Subject_Class_GroupKey = 0,
            bool Opened = false)
        {
            var Subject_Class = from sjc in subject_Class_Group
                                where sjc.Training_Subject_Class_GroupKey == Subject_Class_GroupKey
                                select sjc;
            foreach (var item in Subject_Class)
            {
                Training_Subject_Class_GroupInfo info =
                    new Training_Subject_Class_GroupInfo(Subject_Class_GroupKey);
                if (info != null)
                {
                    info.Properties.Opened = Opened;
                    info.Save(); 
                }
            }
        }
    }
}